home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / Libraries / SAT 2.3b4 / Misc / SATminimalX ƒ / FaceSetFromPICT.p next >
Text File  |  1994-08-23  |  5KB  |  172 lines

  1. {Load a set of faces from PICTs, where a DLOG/DITL -preferrably invisible - is used for telling where}
  2. {each face is located. EXPERIMENTAL!}
  3. {}
  4. {For avoiding to waste too much memory, gSAT.offScreen is used for temorarily storing the image}
  5. {PICT, while a new offscreen is allocated for the mask PICT.}
  6. {}
  7. {NOTE:}
  8. {- The DLOG should have an item #1 that is not used (so you can display a PICT there), and item 2 and up for}
  9. {a set of rectangles, e.g. User Items.}
  10. {- You are responsible for restoring gSAT.offScreen after all faces are loaded, i.e. by CopyBits from gSAT.backScreen.}
  11. {- Your face handler gets the actual DITL index, that is from 2 and up!}
  12. {- This code has practically no error checking, so beware of out of memory or erroneous resource numbers!}
  13.  
  14. unit FaceSetFromPICT;
  15.  
  16. interface
  17.     uses
  18.         SAT;
  19.  
  20.     procedure GetFaceSetFromPICT (dlogId, colorPICTid, bwPICTid, maskPICTid: integer; faceHandler: ProcPtr);
  21.  
  22. implementation
  23.  
  24. {faceHandler should be a pointer to a procedure declared as:}
  25. {procedure MyFaceHandler(theFace:FacePtr;index: integer);}
  26. {In that procedure, you should save the FacePtr in an appropriate place.}
  27.  
  28.     procedure CallFaceHandler (theFace: FacePtr; index: integer; myProc: ProcPtr);
  29.     inline
  30.         $205f,     {movea.l  (a7)+,a0        ; (a0) is theFace, 4(a0) is myProc - I think}
  31.         $4e90;
  32.  
  33.  
  34. {WARNING: No error checking yet!}
  35.  
  36.  
  37.     procedure CreateBWOffScreen (var portP: GrafPtr; rectP: Rect);
  38.         var
  39.             savePort: GrafPtr;
  40.     begin
  41.         GetPort(savePort);
  42.         portP := GrafPtr(NewPtr(sizeof(GrafPort)));
  43.         OpenPort(portP);
  44.         portP^.portRect := rectP;
  45.         portP^.portBits.bounds := portP^.portRect;
  46.  
  47.         RectRgn(portP^.visRgn, rectP);
  48.         ClipRect(rectP);
  49.  
  50.         portP^.portBits.rowBytes := longint(((portP^.portRect.right - portP^.portRect.left + 31) div 32) * 4);
  51.         portP^.portBits.baseAddr := NewPtr(portP^.portBits.rowBytes * longint(portP^.portRect.bottom - portP^.portRect.top));
  52.  
  53.         SetPort(portP);
  54.         EraseRect(portP^.portRect);
  55.  
  56.         SetPort(savePort);
  57.     end;
  58.  
  59.     procedure DisposeBWOffscreen (portP: GrafPtr);
  60.         var
  61.             currPort: GrafPtr;
  62.     begin
  63.         begin
  64.             GetPort(currPort);
  65.             if currPort = portP then
  66.                 begin
  67.          (* It is; set current port to Window Manager GrafPort *)
  68.                     GetWMgrPort(currPort);
  69.                     SetPort(currPort);
  70.                 end;
  71.             ClosePort(portP);
  72.             if portP^.portBits.baseAddr <> nil then
  73.                 DisposPtr(portP^.portBits.baseAddr);
  74.             DisposPtr(Ptr(portP));
  75.         end;
  76.     end;
  77.  
  78.     procedure GetFaceSetFromPICT (dlogId, colorPICTid, bwPICTid, maskPICTid: integer; faceHandler: ProcPtr);
  79.         var
  80.             bounds: Rect;
  81.             thePICT, maskPICT: PicHandle;
  82.             theFace: FacePtr;
  83.             savePort: GrafPtr;
  84.             saveDev: GDhandle;
  85.             theDialog: DialogPtr;
  86.             bwOff: GrafPtr;
  87.             frame, maskFrame: Rect;
  88.             box, zBox: Rect;
  89.             i: integer;
  90.             dlogError: Boolean;
  91.         function GetItemBox (itemNo: integer): Rect;
  92.             var
  93.                 kind: integer;
  94.                 item: Handle;
  95.                 box: Rect;
  96.                 tmpStr: Str255;
  97.         begin
  98.             item := Handle(-1);
  99.             GetDItem(theDialog, itemNo, kind, item, box);
  100.             if item = Handle(-1) then {If unchanged, the item does not exist.}
  101.                 dlogError := true;
  102.             GetItemBox := box;
  103.         end; {GetItemBox}
  104.     begin
  105.         SATGetPort(savePort, saveDev);
  106.  
  107.         theDialog := GetNewDialog(dlogId, nil, WindowPtr(-1));
  108.  
  109. {Get PICTs}
  110. {IDEA: It should really check if the PICT it loads was loaded already, and if it was, don't dispose it.}
  111.         if gSAT.initDepth > 1 then
  112.             thePICT := GetPicture(colorPICTid)
  113.         else
  114.             thePICT := GetPicture(bwPICTid);
  115.         maskPICT := GetPicture(maskPICTid);
  116.         bounds := thePICT^^.picFrame;
  117.         OffsetRect(bounds, -bounds.left, -bounds.top); {onödigt för det gör NewFace åt oss.}
  118.  
  119.         if (thePICT = nil) or (maskPICT = nil) then
  120.             exit(GetFaceSetFromPICT);
  121.  
  122.         frame := thePICT^^.picFrame;
  123.         OffsetRect(frame, -frame.left, -frame.top);
  124.         maskFrame := maskPICT^^.picFrame;
  125.         OffsetRect(maskFrame, -maskFrame.left, -maskFrame.top);
  126.  
  127.         CreateBWOffscreen(bwOff, maskFrame);
  128.         SetPort(bwOff);
  129.         DrawPicture(maskPICT, maskFrame);
  130.         SATSetPort(gSAT.offScreen, gSAT.offScreenGD);
  131.         EraseRect(gSAT.offScreen^.portRect);
  132.         DrawPicture(thePICT, frame);
  133.  
  134.         dlogError := false;
  135.         i := 2;
  136.         while not dlogError do
  137.             begin
  138. {Draw in the face}
  139.                 box := GetItemBox(i);
  140.                 if not dlogError then
  141.                     begin
  142.                         zBox := box;
  143.                         OffsetRect(zBox, -box.left, -box.top);
  144. {Create face}
  145.                         theFace := SATNewFace(zBox);
  146. {Draw face}
  147.                         SATSetPortFace(theFace);
  148.                         CopyBits(gSAT.offScreen^.portBits, GrafPtr(gSAT.iconPort)^.portBits, box, zBox, srcCopy, nil);
  149. {Draw mask}
  150.                         SATSetPortMask(theFace);
  151.                         CopyBits(bwOff^.portBits, GrafPtr(gSAT.bwiconPort)^.portBits, box, zBox, srcCopy, nil);
  152.  
  153. {Tell SAT that we are done}
  154.                         SATChangedFace(theFace);
  155. {Send the face to the host}
  156.                         if faceHandler <> nil then
  157.                             CallFaceHandler(theFace, i, faceHandler);
  158.                         i := i + 1;
  159.                     end;
  160.             end;
  161.  
  162.  
  163. {Get rid of the PICTs}
  164.         ReleaseResource(Handle(thePICT));
  165.         ReleaseResource(Handle(maskPICT));
  166.         DisposeDialog(theDialog);
  167.  
  168.         DisposeBWOffscreen(bwOff);
  169.  
  170.         SATSetPort(savePort, saveDev);
  171.     end;
  172. end.